package com.wyz.dao;



import java.util.List;

import com.wyz.entity.Movie;
import com.wyz.entity.Review;
import com.wyz.entity.Type;
import com.wyz.entity.User;

import my.db.QueryHelper;

/**
 * 针对某一个实体对应表的增删改查
 * 	单个表的增删改查，和业务没有关系
 * */
public class ReviewDao {
		
	/**
	 * 增加影评
	 * */
	public void save(Review review) {
		String sql = "insert into review values(null,?,?,?,?,?,?,?)";
		QueryHelper.update(sql, review.getMovieId(),review.getContent(),review.getRating(),review.getCreateTime(),review.getThumb_upAmount(),review.getTitle(),review.getWriterId());
	}
	/**
	 * 删除影评
	 * */
	public void remove(Long reviewId) {
		String sql = "delete from review where id=?";
		QueryHelper.update(sql, reviewId);
	}
	
	/**
	 * 修改点赞数
	 * */
	public void addThumb_upAmount(Long reviewId,int amount) {
		String sql ="update review set thumb_upAmount = ? where id=?";
		QueryHelper.update(sql,amount,reviewId);
		
	}
	
	/**
	 * 查询影评
	 * */
	public List<Review> findAll() {
		String sql = "select * from review";
		List<Review> list =QueryHelper.query(Review.class, sql);
		QueryHelper.load(list, "movie", "movieId", Movie.class);
		QueryHelper.load(list, "writer", "writerId", User.class);
		return list;
	}
	
	
	public List<Review> findByUserId(Long userId) {
		String sql = "select * from review where writerId=?";
		List<Review> list =QueryHelper.query(Review.class, sql,userId);
		QueryHelper.load(list, "movie", "movieId", Movie.class);
		QueryHelper.load(list, "writer", "writerId", User.class);
		return list;
	}
	
	public List<Review> findByMovieId(Long movieId) {
		String sql = "select * from review where movieId=?";
		List<Review> list =QueryHelper.query(Review.class, sql,movieId);
		QueryHelper.load(list, "movie", "movieId", Movie.class);
		QueryHelper.load(list, "writer", "writerId", User.class);
		return list;
	}
	
	public Review findById(Long id) {
		String sql = "select * from review where id=?";
		Review review=QueryHelper.queryUnique(Review.class, sql, id);
		QueryHelper.load(review, "movie", "movieId", Movie.class);
		QueryHelper.load(review, "writer", "writerId", User.class);
		return review;
	}
	
	
	
	public static void main(String[] args) {
		Review review=new Review();
		ReviewDao dao=new ReviewDao();
		dao.save(review);
		dao.save(review);
		dao.remove(1L);
		dao.addThumb_upAmount(2L, 12);
		System.out.println(review.getCreateTime());
		
		List<Review> reviews=dao.findAll();
		for(int i=0;i<reviews.size();i++) {
			System.out.println(reviews.get(i).getContent());
		}
	}
}
